var app = getApp();
Page({
  
  data: {
    userInfo: null,
    archivesInfo: { article: {} },
    commentList: [],
    money:'',
    id:0,
    aid:0,
    is_collection:0,
    loading: false,
    nodata: false,
    nomore: false,
    form: { quotepid: 0, message: '',complaint: '', report:'',focus: false }
  },
  page: 1,

  onLoad: function (options) {
    var that = this;
      if(options.id){
          that.data.id = options.id;
      }
      that.setData({ userInfo: app.globalData.userInfo });
    app.request('/archives/detail', { id: that.data.id }, function (data, ret) {
      var content = data.archivesInfo.content;
      data.archivesInfo.article = app.towxml.toJson(content, 'html');
      data.commentList.forEach(function (item) {
        item.article = app.towxml.toJson(item.content, 'html');
      });
      that.setData({ archivesInfo: data.archivesInfo, channelInfo: data.channelInfo, commentList: data.commentList,is_collection: data.archivesInfo.is_collection});
      that.page++;
    }, function (data, ret) {
      app.error(ret.msg);
    });
  },
  reply: function (event) {
    var that = this;
    var pid = event.currentTarget.dataset.pid;
    var nickname = event.currentTarget.dataset.nickname;
    that.setData({ form: { quotepid: pid, message: '@' + nickname + ' ', focus: true } });
  },
    del_comment: function (event) {
        var that = this;
        var cid = event.currentTarget.dataset.cid;
        var aid = event.currentTarget.dataset.aid;

        app.request('/comment/del', { cid: cid, aid: aid}, function (data) {
            app.success('删除成功!');
            that.setData({ form: { quotepid: 0, message: '', focus: false }, commentList: [], nodata: false, nomore: false });
            if (that.data.commentList.length < 10) {
                that.page = 1;
            } else {
                that.data.commentList = that.data.commentList.slice(0, 10);
                that.page = 2;
            }
            that.onReachBottom();
        }, function (data, ret) {
            app.error(ret.msg);
        });
    },
  login: function (event) {
    var that = this;
    app.login(function (data) {
      that.setData({ userInfo: app.globalData.userInfo });
    });
  },
    collection: function (event) {
        var that = this;
        var id = event.currentTarget.dataset.id;
        var type = event.currentTarget.dataset.type;
        app.request('/archives/collection', { id: id, type: type }, function (data, ret) {
            wx.showToast({
                title: '收藏成功',
                icon: 'succes',
                duration: 1000,
                mask:true
            })
            that.setData({ is_collection: 1 });
        }, function (data, ret) {
            app.error(ret.msg);
        });
    },
    qu_collection: function (event) {
        var that = this;
        var id = event.currentTarget.dataset.id;
        var type = event.currentTarget.dataset.type;
        app.request("/archives/qu_collection", { id: id }, function (data, ret) {
            wx.showToast({
                title: '取消收藏成功',
                icon: 'succes',
                duration: 1000,
                mask:true
            })
            that.setData({ is_collection: 0 });
        }, function (data, ret) {
            app.error(ret.msg);
        });
    },
  formSubmit: function (event) {
    var that = this;
    var pid = event.currentTarget.dataset.pid;
    if (!app.globalData.userInfo) {
      app.error('请登录后再评论');
      return;
    }
    if (event.detail.value.message == '') {
      app.error('内容不能为空');
      return;
    }
    app.request('/comment/post', { aid: this.data.archivesInfo.id, pid: this.data.form.quotepid, username: event.detail.value.username, content: event.detail.value.content }, function (data) {
      app.success('发表成功!');
      that.setData({ form: { quotepid: 0, message: '', focus: false }, commentList: [], nodata: false, nomore: false });
      if (that.data.commentList.length < 10) {
        that.page = 1;
      } else {
        that.data.commentList = that.data.commentList.slice(0, 10);
        that.page = 2;
      }
      that.onReachBottom();
    }, function (data, ret) {
      app.error(ret.msg);
    });
  },
    formComplaint: function (event) {
        var that = this;
        var pid = event.currentTarget.dataset.pid;
        if (!app.globalData.userInfo) {
            app.error('请登录后再投诉');
            return;
        }
        if (event.detail.value.complaint == '') {
            app.error('内容不能为空');
            return;
        }
        app.request('/complaint/post', { aid: this.data.archivesInfo.id, pid: this.data.form.quotepid, username: event.detail.value.username, content: event.detail.value.complaintContent }, function (data) {
            app.success('投诉成功!');
            that.setData({ form: { quotepid: 0, message: '', focus: false }, commentList: [], nodata: false, nomore: false });
            if (that.data.commentList.length < 10) {
                that.page = 1;
            } else {
                that.data.commentList = that.data.commentList.slice(0, 10);
                that.page = 2;
            }
            that.onReachBottom();
        }, function (data, ret) {
            app.error(ret.msg);
        });
    },
    formReport: function (event) {
        var that = this;
        var pid = event.currentTarget.dataset.pid;
        if (!app.globalData.userInfo) {
            app.error('请登录后再举报');
            return;
        }
        if (event.detail.value.complaint == '') {
            app.error('内容不能为空');
            return;
        }
        app.request('/report/post', { aid: this.data.archivesInfo.id, pid: this.data.form.quotepid, username: event.detail.value.username, content: event.detail.value.reportContent }, function (data) {
            app.success('举报成功!');
            that.setData({ form: { quotepid: 0, message: '', focus: false }, commentList: [], nodata: false, nomore: false });
            if (that.data.commentList.length < 10) {
                that.page = 1;
            } else {
                that.data.commentList = that.data.commentList.slice(0, 10);
                that.page = 2;
            }
            that.onReachBottom();
        }, function (data, ret) {
            app.error(ret.msg);
        });
    },
  vote: function (event) {
    var that = this;
    app.vote(event, function (data) {
      that.setData({ ['archivesInfo.likes']: data.likes, ['archivesInfo.dislikes']: data.dislikes, ['archivesInfo.likeratio']: data.likeratio });
    });
  },
    /**
     * 弹窗
     */
    showDialogBtn: function (event) {
        var aid = event.currentTarget.dataset.id;
        this.setData({
            aid: aid,
            showModal: true
        })
    },
    /**
     * 弹出框蒙层截断touchmove事件
     */
    preventTouchMove: function () {
    },
    /**
     * 隐藏模态对话框
     */
    hideModal: function () {
        this.setData({
            showModal: false
        });
    },
    /**
     * 对话框取消按钮点击事件
     */
    onCancel: function () {
        this.hideModal();
    },
    /**
     * 获取评论内容
     */
    getPinglun: function (e) {
        var val = e.detail.value;
        this.setData({
            money: val
        });
    },
    /**
     * 对话框确认按钮点击事件
     */
    onConfirm: function (event) {
        var that = this;
        var id = that.data.aid;
        var m = that.data.money;

        if (m == '') {
            app.error('请输入筑梦金额');
            return;
        }

        app.request("/order/submit", { id: id,money:m }, function (data, ret) {
            wx.requestPayment({
                timeStamp: data.data.timeStamp,
                nonceStr: data.data.nonceStr,
                package: data.data.package,
                signType: data.data.signType,
                paySign: data.data.paySign,
                success: function (res) {
                    // success
                    console.log(res);
                    that.onLoad({'id':that.data.id});
                },
                fail: function (res) {
                    // fail
                    console.log(res);
                    that.onLoad({'id':that.data.id});
                },
                complete: function (res) {
                    // complete
                    console.log(res);
                    that.onLoad({'id':that.data.id});
                }
            })
        }, function (data, ret) {
            app.error(ret.msg);
        });

        this.hideModal();
    },
  onReachBottom: function () {
    var that = this;
    this.loadComment(function (data) {
      if (data.commentList.length == 0) {
        //app.info("暂无更多数据");
      }
    });
  },
  loadComment: function (cb) {
    var that = this;
    if (that.data.nomore == true || that.data.loading == true) {
      return;
    }
    this.setData({ loading: true });
    app.request('/comment', { aid: this.data.archivesInfo.id, page: this.page }, function (data, ret) {
      data.commentList.forEach(function (item) {
        item.article = app.towxml.toJson(item.content, 'html');
      });
      that.setData({
        loading: false,
        nodata: that.page == 1 && data.commentList.length == 0 ? true : false,
        nomore: that.page > 1 && data.commentList.length == 0 ? true : false,
        commentList: that.page > 1 ? that.data.commentList.concat(data.commentList) : data.commentList,
      });
      that.page++;
      typeof cb == 'function' && cb(data);
    }, function (data, ret) {
      that.setData({
        loading: false
      });
      app.error(ret.msg);
    });
  },
    //点击播放录音
    gotoPlay: function (e) {
        var filePath = e.currentTarget.dataset.key;

        //点击开始播放
        const innerAudioContext = wx.createInnerAudioContext()
        innerAudioContext.autoplay = true;
        innerAudioContext.autoplay = true;
        innerAudioContext.src = filePath;
        innerAudioContext.play();
        innerAudioContext.onPlay(() => {
            wx.showToast({
                title: '开始播放',
                icon: 'success',
                duration: 1000
            })
        })
        innerAudioContext.onError((res) => {
            console.log(res.errMsg)
            console.log(res.errCode)
        })
    },
  share: function () {
    wx.showShareMenu({});
  },
  onShareAppMessage: function () {
    return {
      title: this.data.archivesInfo.title,
      desc: this.data.archivesInfo.intro,
      path: '/page/news/detail?id=' + this.data.archivesInfo.id
    }
  },
})